package com.spongeli.user.aop;

import java.lang.reflect.Method;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import com.spongeli.user.datasource.DataSource;
import com.spongeli.user.datasource.DataSourceCons;
import com.spongeli.user.datasource.DataSourceHolder;

@Component
public class DataSourceAspect {

	public void changeDateSource(JoinPoint point) {
		MethodSignature signtrue = (MethodSignature)point.getSignature();
		Class<?>[] classes = point.getTarget().getClass().getInterfaces();
		Class<?>[] parameterTypes = signtrue.getMethod().getParameterTypes();
		try {
			Method method = classes[0].getMethod(signtrue.getMethod().getName(), parameterTypes);
			if(method != null && method.isAnnotationPresent(DataSource.class)){
				DataSource dataSource = method.getAnnotation(DataSource.class);
				System.out.println("当前切换数据源为："+dataSource.value());
				DataSourceHolder.setCustomeType(dataSource.value());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public void returnDateSource(JoinPoint point) {
		DataSourceHolder.setCustomeType(DataSourceCons.test);
	}
}
